package org.insia.smartposse.struts.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.config.DAOFactory;
import org.insia.smartposse.metier.Company;
import org.insia.smartposse.metier.Freelance;
import org.insia.smartposse.metier.User;
import org.insia.smartposse.utils.ActionUtils;
import org.insia.smartposse.utils.PasswordEncoder;
/**
 * Modify Profile Action
 * @author sok hout
 *
 */
public class ModifyProfileAction extends DispatchAction {
	protected final Logger log = Logger.getLogger(ModifyProfileAction.class);

    public ActionForward modifyProfile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) 
    throws Exception {
    	// initialisation de la liste des mesages d'erreurs
    	List<String> messages = new ArrayList<String>();
    	request.setAttribute("messages", messages);
    	User user = (User)request.getSession().getAttribute("user");
    		
    	// recupération des données
    	Long id = ActionUtils.getLong(request, "id", -1L, messages, "profile.error.id");
    	Integer gender = ActionUtils.getInt(request, "gender", 0, messages, "profile.error.gender");
    	String firstName = ActionUtils.getTrimedString(request, "firstName","", messages, "profile.error.firstName");
    	String lastName = ActionUtils.getTrimedString(request, "lastName","", messages, "profile.error.lastName");
    	String login = ActionUtils.getTrimedString(request, "login","", messages, "profile.error.login");
    	String password = ActionUtils.getTrimedString(request, "password","", null, null);
    	String email = ActionUtils.getEmail(request, "email","", messages, "profile.error.email");

    	Freelance freelance = (Freelance)request.getSession().getAttribute("freelance");
    	// Freelance's information
    	if (freelance != null) {
    		Double fee = ActionUtils.getDouble(request, "fee",0.0, messages, "profile.error.fee");
        	String description = ActionUtils.getTrimedString(request, "description","", null,null);
        	
        	// S'il y a eu des erreurs
        	if (messages.size() > 0) {
        		freelance = new Freelance();
            	freelance.setParams(id.longValue(),login,password,firstName,lastName,gender.intValue(),email,description,fee.doubleValue());
        		request.setAttribute("freelance", freelance);
        		request.setAttribute("user", (User)freelance);
        		return mapping.findForward("show");
        	}
        	freelance.setParams(id.longValue(),login,password.length()==0?user.getPassword():PasswordEncoder.encode(password),firstName,lastName,gender.intValue(),email,description,fee.doubleValue());
    		DAOFactory.getFreelanceDAO().update(freelance);
    	} else {
	    	// Company's information
	    	String url = ActionUtils.getString(request, "url","", null, null);
	    	String name = ActionUtils.getString(request, "name","", messages, "profile.error.name");
	    	String address = ActionUtils.getString(request, "address","", null, null);
	    	Company company = (Company)request.getSession().getAttribute("company");
	    	
	    	// S'il y a eu des erreurs
	    	if (messages.size() > 0) {
	    		company = new Company();
		    	company.setParams(id, login, password, firstName, lastName, gender, email, name, address, url);
	    		request.setAttribute("company", company);
	    		request.setAttribute("user", (User)company);
	    		return mapping.findForward("show");
	    	}
	    	company.setParams(id, login, password.length()==0?user.getPassword(): PasswordEncoder.encode(password), firstName, lastName, gender, email, name, address, url);
    		DAOFactory.getCompanyDAO().update(company);
    	}
		request.setAttribute("msgTitleKey", "profile.updateSuccessTitle");
		request.setAttribute("msgLabelKey", "profile.updateSuccessLabel");

    	return mapping.findForward("messagePage");
    }

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
	throws Exception {
		return mapping.findForward("show");
	}
}
